import {useTableActionRedux} from "@/components/ant-ext-table/redux/HAdminContext";
import {useActionForm} from "@/common/hooks";
import HModal from "@/components/ant-ext/HModal";
import {Form} from "antd";
import HFormItem from "@/components/ant-ext/HFormItem";
import {AdminType,AdminSex,AdminStat} from "@/const/const";
import SysAdmin, {SysAdminEntity} from "@/api/sys-admin";
import {useEffect, useState} from "react";

export default function AdminEdit() {
    const [loading, setLoading] = useState(false)
    const {action, reset} = useTableActionRedux<SysAdminEntity>()
    const {form, key, title} = useActionForm(action, [SysAdmin.EDIT], SysAdmin.MAPPING)
    const close = () => {
        reset()
        form.resetFields()
    }
    useEffect(() => {
        if (action.match(SysAdmin.EDIT)) {
            // SysAdmin.selectById({
            //     params: {code: action.getRow()?.adminCode}, setLoading, success: (data) => {
            //         form.setFieldsValue(data)
            //         setExpandedAuthOrgCodeKeys(data.authOrgCode)
            //         setExpandedOrgCodeKeys([data.orgCode])
            //     }
            // })
        }
    }, [action, form])


    return <>
        <HModal onCancel={close} type={"setUp"} width={500} onOk={form.submit}
                confirmLoading={loading}
                visible={title !== undefined}
                title={title}
        >

            <Form name={"editForm"} form={form} labelCol={{span: 5}}
                  onFinish={() => {
                      SysAdmin.doEdit({
                          body: form.getFieldsValue(), setLoading, success: () => {
                              close()
                          }
                      })
                  }}>
                <HFormItem label={"id"} name={"id"} hidden={key === SysAdmin.ADD}
                           disabled={key !== SysAdmin.ADD}/>
                <HFormItem label={"用户名称"} name={"username"} required/>
                <HFormItem label={"真实姓名"} name={"realName"} required/>
                <HFormItem label={"登陆密码"} name={"password"} required/>
                <HFormItem label={"手机号"} name={"mobile"} required/>
                <HFormItem label={"用户性别"} name={"sex"} type={"select"} dataSource={AdminSex.select} required/>
                <HFormItem label={"用户类型"} name={"type"} type={"select"} dataSource={AdminType.select} required/>
                <HFormItem label={"状态"} type={"radio"} dataSource={AdminStat.select} name={"status"}
                           required/>
                
                {/* <HFormItem label={"隶属组织"} name={"orgCode"} type={"treeSelect"}
                           treeExpandedKeys={expandedOrgCodeKeys} onTreeExpand={setExpandedOrgCodeKeys}
                           dataSource={SysDepart.selectTree}
                           required/>
                <HFormItem label={"数据角色"} name={"authOrgCode"} type={"treeSelect"} multiple maxTagCount={1}
                           treeExpandedKeys={expandedAuthOrgCodeKeys} onTreeExpand={setExpandedAuthOrgCodeKeys}
                           dataSource={SysDepart.selectTree}/>*/}

            </Form>
        </HModal>
    </>
}